<?php

namespace app\admin\controller;
use think\Db;
use think\Controller;
use think\Request;

class Con1 extends  Login
{
    public function index()
    {   
             // var_dump($_SESSION);die;
        $list = Db::table('hc_user')->field(['id', 'name'])->order(['id' => 'ASC'])->select();
//        var_dump($list);
        return view('admin@con1/index', [
            'title' => '用户列表',
            'list' => $list
        ]);
    }

    public function func1()
    {
        return view('adduser', [
            'title' => '添加用户',
           
        ]);
    }

    public function doadduser(Request $request)
    {   
        // var_dump($request);
      $p = $request->post();
         // 手动处理
        $data = [
            'name' => $p['name'],
            'pass' => md5($p['pass'])
        ];
          $result = Db::table('hc_user')->data($data)->insert();

          if ($result > 0) {
            return $this->success('安排上了~', url('/admin/user'));
        } else {
            return $this->error('还未安排上,请换个姿势');
        }
       // var_dump('这里已经到了添加准备SQL方法的一步了');
    }

     public function readpage($id)
    {

        $sql = "SELECT * FROM `hc_juese`";
        $role = Db::query($sql);
        // var_dump($role);
        $data = Db::table('hc_uj')->field('uid')->where('id = '.$id)->select();
        // var_dump($data);
        foreach ($role as $k => $v) {
           
          $role[$k]['check'] = null;
            foreach ($data as $u=>$c) {
                if ($v['uid'] == $c['uid']) {
                    $role[$k]['check'] = 'checked';

                }
            }
        }
        // var_dump($role);die;


        return view('adduj',[
            'role' => $role,
            'id' =>$id
            ]);
    }

    public function  doadduj()
    { 

    
      $p = Request::instance()->post();
     
     
      
      $data['id'] = $p['id'];
   
         $data['uid'] = $p['rid'];
       Db::name('hc_uj')->where('id',$data['id'])->delete(); 

      foreach ($data['uid'] as $key => $v) {
         $list = [
                    'id' => $data['id'],
                    'uid' => $v
                ];
        
        // var_dump($data);die;
         $result = Db::name('hc_uj')->insert($list);


      }
      if ($result > 0 ){
            return $this->success('新增成功',url('/admin/user'));
        }else{
            return $this->success('新增失败');
        }




      // var_dump('到做添加角色的控制器了');
    }




     public function xiangqing($id)
     {    
        // var_dump($id);
        $ron = Db::table('hc_user')
            ->find($id);
        $row = Db::table('hc_user')
            ->alias('ro')

            ->join('hc_uj q','q.id = ro.id')
            ->join('hc_juese ta','q.uid = ta.uid')
            ->select($id);


         $ros = Db::table('hc_user')
            ->alias('ro')

            ->join('hc_uj q','q.id = ro.id')
            ->join('hc_juese ta','q.uid = ta.uid')
            ->join('hc_jj jj','jj.uid = ta.uid')
            ->join('hc_jiedian jd','jj.cid = jd.cid')

            ->select($id);    


           // $ros=$row;
            // dump($row);
        // var_dump($ron);die;
             return view('xiangqing',[
            'row' => $row,
            'ros' => $ros,
            'ron' => $ron

            
            ]);

      // var_dump('6666');die;
     }

}